package com.nidu.demo.dept.executor.query;

import com.alibaba.cola.dto.SingleResponse;
import com.alibaba.cola.exception.BizException;
import com.nidu.demo.common.exception.ErrorCodeConstants;
import com.nidu.demo.dept.convertor.DepartmentConvertor;
import com.nidu.demo.dept.dto.DepartmentCO;
import com.nidu.demo.dept.gateway.DepartmentGateway;
import com.nidu.demo.dept.model.Department;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

/**
 * 部门单查询执行器
 *
 * @author auto
 */
@Component
@RequiredArgsConstructor
public class DepartmentSingleQryExe {

    private final DepartmentGateway gateway;

    private final DepartmentConvertor convertor;

    public SingleResponse<DepartmentCO> execute(Long id) {
        // 1. 查询数据
        Department dept = gateway.getById(id);
        if(dept == null){
            throw new BizException(ErrorCodeConstants.DEPARTMENT_NOT_FOUND.getCode(), ErrorCodeConstants.DEPARTMENT_NOT_FOUND.getMessage());
        }
        // 2. 转换为DTO返回
        DepartmentCO DepartmentCO = convertor.toClientObject(dept);

        return SingleResponse.of(DepartmentCO);
    }
    
}